package TestExercises07;

//多线程练习1(卖电影票)

/*
一共有1000张电影票,可以在两个窗口领取,假设每次领取的时间为3000毫秒
要求:请用多线程模拟卖票过程并打印剩余电影票的数量
*/

public class Test01 {
    public static void main(String[] args) {

        //创建线程对象
        MyThread01 t1 = new MyThread01();
        MyThread01 t2 = new MyThread01();

        //设置线程名字
        t1.setName("窗口1");
        t2.setName("窗口2");

        //开启线程
        t1.start();
        t2.start();
    }
}

class MyThread01 extends Thread {

    static int ticket = 1000;

    static Object obj = new Object();

    @Override
    public void run() {
        while (true) synchronized (obj) {
            if (ticket > 0) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(getName() + "卖的的第" + (1001 - ticket) + "张票");
                ticket--;
                System.out.println("剩余" + ticket + "张票");
            } else {
                return;
            }
        }
    }
}
